<?php
require dirname(__FILE__).'/function.php';

function socket_post($url, $post) {
    $urls = parse_url($url);
    if (!isset($urls['port'])) {
        $urls['port'] = 80;
    }

    $fp = fsockopen($urls['host'], $urls['port'], $errno, $errstr);
    if (!$fp) {
        echo "$errno, $errstr";
        exit();
    }

    $post = http_build_query($post);
    $length = strlen($post);
    $header = <<<HEADER
POST {$urls['path']} HTTP/1.1
Host: {$urls['host']}
Content-Type: application/x-www-form-urlencoded
Content-Length: {$length}
Connection: close

{$post}
HEADER;

    fwrite($fp, $header);
    $result = '';
    while (!feof($fp)) {
        // receive the results of the request
        $result .= fread($fp, 512);
    }
    $result = explode("\r\n\r\n", $result, 2);
    return $result[1];
}

$data = socket_post("http://localhost/phplearn/postmethod/post.php", array('name'=>'caiknife', 'email'=>'caiknife@gmail.com'));

echo $data ;
//测试方法：
//http://localhost/phplearn/postmethod/post_socket.php
